﻿using Microsoft.AspNetCore.Mvc;
using Model;
using Service;
using System.Linq.Expressions;

namespace AME.WbApi.Controllers
{

    [ApiController]
    [Route("api/[controller]")]
    public class UserController : ControllerBase
    {
        private readonly UserService _userService;

        public UserController(UserService userService)
        {
            _userService = userService;
        }

        // 添加单个用户
        [HttpPost("add")]
        public async Task<IActionResult> AddUser([FromBody] UserModel user)
        {
            var result = await _userService.AddUserAsync(user);
            if (result)
            {
                return Ok(new { message = "User created successfully" });
            }
            return BadRequest(new { message = "Failed to create user" });
        }

        // 批量添加用户
        [HttpPost("add-multiple")]
        public async Task<IActionResult> AddUsers([FromBody] List<UserModel> users)
        {
            var result = await _userService.AddUsersAsync(users);
            if (result)
            {
                return Ok(new { message = "Users created successfully" });
            }
            return BadRequest(new { message = "Failed to create users" });
        }

        // 更新用户
        [HttpPut("update")]
        public async Task<IActionResult> UpdateUser([FromBody] UserModel user)
        {
            var result = await _userService.UpdateUserAsync(user);
            if (result)
            {
                return Ok(new { message = "User updated successfully" });
            }
            return BadRequest(new { message = "Failed to update user" });
        }

        // 根据 ID 删除用户
        [HttpDelete("delete/{id}")]
        public async Task<IActionResult> DeleteUserById(int id)
        {
            var result = await _userService.DeleteUserByIdAsync(id);
            if (result)
            {
                return Ok(new { message = "User deleted successfully" });
            }
            return BadRequest(new { message = "Failed to delete user" });
        }

        // 根据条件删除用户（例如根据状态删除未激活用户）
        [HttpDelete("delete")]
        public async Task<IActionResult> DeleteUsers([FromBody] bool isActive)
        {
            Expression<Func<UserModel, bool>> condition = user => user.IsActive == isActive;
            var result = await _userService.DeleteUsersAsync(condition);
            if (result)
            {
                return Ok(new { message = "Users deleted successfully" });
            }
            return BadRequest(new { message = "Failed to delete users" });
        }

        // 根据 ID 获取用户
        [HttpGet("get/{id}")]
        public async Task<IActionResult> GetUserById(int id)
        {
            var user = await _userService.GetUserByIdAsync(id);
            if (user != null)
            {
                return Ok(user);
            }
            return NotFound(new { message = "User not found" });
        }

        // 获取所有用户
        [HttpGet("get-all")]
        public async Task<IActionResult> GetAllUsers()
        {
            var users = await _userService.GetAllUsersAsync();
            return Ok(users);
        }

        // 根据条件获取用户列表
        [HttpGet("get-by-condition")]
        public async Task<IActionResult> GetUsersByCondition([FromQuery] bool isActive)
        {
            Expression<Func<UserModel, bool>> condition = user => user.IsActive == isActive;
            var users = await _userService.GetUsersByConditionAsync(condition);
            return Ok(users);
        }

        // 根据条件判断用户是否存在
        [HttpGet("exists")]
        public async Task<IActionResult> AnyUser([FromQuery] bool isActive)
        {
            Expression<Func<UserModel, bool>> condition = user => user.IsActive == isActive;
            var exists = await _userService.AnyUserAsync(condition);
            return Ok(new { exists });
        }

        // 根据条件获取单个用户
        [HttpGet("get-single")]
        public async Task<IActionResult> GetSingleUser([FromQuery] bool isActive)
        {
            Expression<Func<UserModel, bool>> condition = user => user.IsActive == isActive;
            var user = await _userService.GetSingleUserAsync(condition);
            if (user != null)
            {
                return Ok(user);
            }
            return NotFound(new { message = "User not found" });
        }

        // 分页获取用户列表
        [HttpGet("get-paged")]
        public async Task<IActionResult> GetPagedUsers([FromBody] UserModel request,int pageIndex, int pageSize)
        {
            // 分页查询
            var (users, totalCount) = await _userService.GetPagedUsersAsync(request, pageIndex, pageSize);
            return Ok(new { users, totalCount });
        }
    }
}
